package org.jcc.core.cs.functional;

public class PolymorphicAnonymousSum {
	
	public static void main(String[] args) {
		// 匿名类方式
		int idSum = sum(new MyFunction() {
			@Override
			public int apply(int i) {
				return i;
			}
		});
		
		int sqSum = sum(new MyFunction() {
			@Override
			public int apply(int i) {
				return i * i;
			}
		});
		
		int cbSum = sum(new MyFunction() {
			@Override
			public int apply(int i) {
				return i * i * i;
			}
		});
		
		System.out.println(idSum + " " + sqSum + " " + cbSum);
	}

	public static int sum(MyFunction f) {
		int sum = 0;
		for (int i = 1; i <= 100; i++) {
			sum += f.apply(i);
		}
		return sum;
	}
}